import React from 'react'
import { Button, Input, Form } from 'react-vant'
import {useDispatch,useSelector} from "react-redux"
import {useNavigate,useLocation} from "react-router-dom"
import {useEffect} from "react"
 import * as action from "../action/index"
let Addaddress =  () => {
  const navigate = useNavigate()
  const location = useLocation()
  
  
  const [form] = Form.useForm()
  const dispatch = useDispatch()


  const onFinish = values => {
    console.log(values)
    if (location.state.isadd === "新增") {
        dispatch(action.addressfn(values))
    } else {
        dispatch(action.editaddressfn(Object.assign({},values,{editindex:location.state.index})))
    }
  
    navigate("/home/address")

  }
 useEffect(()=>{

     if (location.state.isadd === "编辑")  form.setFieldsValue(location.state.item)
    
 },[])
  return (
    <Form
      form={form}
      onFinish={onFinish}
      footer={
        <div style={{ margin: '16px 16px 0' }}>
          <Button round nativeType='submit' type='primary' block>
            提交
          </Button>
        </div>
      }
    >
      <Form.Item
        intro='确保这是唯一的用户名'
        rules={[{ required: true, message: '请填写用户名' }]}
        name='name'
        label='姓名'
      >
        <Input placeholder='请输入用户名' />
      </Form.Item>
      <Form.Item
        rules={[{ required: true, message: '请填写密码' }]}
        name='address'
        label='住址'
      >
        <Input placeholder='请输入密码' />
      </Form.Item>
      <Form.Item
        rules={[{ required: true, message: '请填写密码' }]}
        name='phone'
        label='电话'
      >
        <Input placeholder='请输入密码' />
      </Form.Item>
    </Form>
  )
}

export default Addaddress